<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Club;
use App\Picture;
use App\Event;

class ClubController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $club = Club::get();
        return view('club.index',compact('club'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $req ,$type)

    {
        if(empty($_POST)) {   //如果没有POST提交数据则显示创建活动页面
            return view('event.create', compact('type'));   
        } else {
            $file = $req->file('pic');  //获取图片对象
            $date = date('Ymd');
            $destPath = realpath('images').$date;  //根据年月日八位数字为文件夹名字保存图片
            
            if(!$is_dir($destPath)) {
                mkdir($destPath, 0777, true);     //如该文件夹不存在则创建一个
            }
            $fileName= str_random(8).$file->getClientOriginalName();  //给上传文件重新命名
            $file->move($destPath, $fileName);    //把上传图片移动到指定目录

            $picture = new Picture();          //获取图片表的对象
            $picture->name = '标题图片';
            $destPath = '/images/'.$date.'/';
            $picture->src = $destPath.$fileName;  //图片路径
            $picture->url = '#';                  //图片超链接地址
            $picture->type = '#';
            $picture->uses = '0';                //图片使用区域  0为活动图片  不参与热图展播
            $picture->status = '0';              //图片状态
            $picture->save();
            $pid = $picture->id;                 //本次保存图片所产生的主键id

            $content = new Content();            //获取活动内容表对象
            $content->content = $_POST['content'];
            $cid              = $content->id;

            $event = new Event();
            $event->act_type = $type;
            $event->title    = $_POST['title'];
            $event->status   = '0';
            $event->pid      = $pid;
            $event->cid      = $cid;
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)

    {
        if(empty($_POST)) {   //如果没有POST提交数据则显示创建活动页面
            $club = Club::find($id);
            $act_type    = $club['act_type'];
            $pid     = $club['pid'];
            $cid     = $club['cid'];
            $picture = Picture::find($pid);
            $content   = Content::find($cid);
            return view('event.update', compact('event', 'picture', 'content'));   
        } else {
            $file = $req->file('pic');  //获取图片对象
            $date = date('Ymd');
            $destPath = realpath('images').$date;  //根据年月日八位数字为文件夹名字保存图片
            
            if(!$is_dir($destPath)) {
                mkdir($destPath, 0777, true);     //如该文件夹不存在则创建一个
            }
            $fileName= str_random(8).$file->getClientOriginalName();  //给上传文件重新命名
            $file->move($destPath, $fileName);    //把上传图片移动到指定目录

            $picture = new Picture();          //获取图片表的对象
            $picture->name = '标题图片';
            $destPath = '/images/'.$date.'/';
            $picture->src = $destPath.$fileName;  //图片路径
            $picture->url = '#';                  //图片超链接地址
            $picture->uses = '0';                //图片使用区域  0为活动图片  不参与热图展播
            $picture->status = '0';              //图片状态
            $picture->save();
            $pid = $picture->id;                 //本次保存图片所产生的主键id

            $content = new Content();            //获取活动内容表对象
            $content->content = $_POST['content'];
            $content->save();
            $cid = $content->id;

            $club = new Club();
            $club->act_type = $type;
            $club->title    = $_POST['title'];
            $club->status   = '0';
            $club->pid      = $pid;
            $club->cid      = $cid;
            $club->save();
        }
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $club = Club::find($id);
        $club->delete();
    }
}
